package C044;

import java.io.*;

/**
 * ClassName: C01
 * Package: C043
 * Description:
 *
 * @Author BCXJ
 * @Create 2025/2/5 09:22
 * @Version 1.0
 * @Since 1.0
 */
public class C01 {

    public static int m, op;

    public static String[] splits;
    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
        String line = null;

        while ((line = in.readLine()) != null) {
            m = Integer.valueOf(line);
            Trie trieNode = new Trie();
            for (int i = 1; i <= m; i++) {
                splits = in.readLine().split(" ");
                op = Integer.valueOf(splits[0]);
                if (op == 1) {
                    trieNode.insert(splits[1]);
                } else if (op == 2) {
                    trieNode.delete(splits[1]);
                } else if (op == 3) {
                    out.println(trieNode.search(splits[1]) > 0 ? "YES" : "NO");
                    System.out.println(trieNode.search(splits[1]) > 0 ? "YES" : "NO");
                } else if (op == 4) {
                    out.println(trieNode.prefixNumber(splits[1]));
                    System.out.println(trieNode.prefixNumber(splits[1]));
                }
            }
        }
        out.flush();
        in.close();
        out.close();
    }


    static class Trie {


        static class TrieNode {
            int pass;
            int end;
            TrieNode[] child = new TrieNode[26];
        }

        //根节点
        TrieNode root;
        public Trie() {
            root = new TrieNode();
        }

        // 前缀树增加节点
        void insert(String word) {
            TrieNode cur = root;
            char[] charArr = word.toCharArray();
            int index;
            for (int i = 0; i < charArr.length; i++) {
                index = charArr[i] - 'a';
                if(cur.child[index] == null) {
                    cur.child[index] = new TrieNode();
                }
                cur.child[index].pass++;
                cur = cur.child[index];
            }
            cur.end++;
        }

        // 前缀树查询节点
        int search(String word) {
            char[] charArr = word.toCharArray();
            TrieNode cur = root;
            int index;
            for (int i = 0; i < charArr.length; i++) {
                index = charArr[i] - 'a';
                if (cur.child[index] == null) {
                    return 0;
                }
                cur = cur.child[index];
            }
            return cur.end;
        }

        // 返回前缀树中以prefix为前缀的字符串个数
        int prefixNumber(String prefix) {
            char[] charArr = prefix.toCharArray();
            TrieNode cur = root;
            int index;
            for (int i = 0; i < charArr.length; i++) {
                index = charArr[i] - 'a';
                if (cur.child[index] == null) {
                    return 0;
                }
                cur = cur.child[index];
            }
            return cur.pass;
        }

        //  从前缀树中移除字符串 word 。
        void delete(String word) {
            // 先查询是否有word单词
            int search = search(word);
            if(search == 0) {
                return;
            }
            TrieNode cur = root;
            cur.pass --;
            char[] charArr = word.toCharArray();
            int index;
            for (int i = 0; i < charArr.length; i++) {
                index = charArr[i] - 'a';
                if (cur.child[index] == null) {
                    return;
                }
                if(cur.child[index].pass == 0) {
                    cur.child[index] = null;
                    return;
                }
                cur = cur.child[index];
                cur.pass --;
            }
            cur.end --;
        }
    }



}
